<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>lua-cassandra documentation</title>
    <link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>lua-cassandra</h1>

<ul>
  <li><a href="../index.html">Index</a></li>
</ul>



<h2>Examples</h2>
<ul class="nowrap">
  <li><a href="../examples/authentication.lua.html">authentication.lua</a></li>
  <li><strong>batch.lua</strong></li>
  <li><a href="../examples/intro.lua.html">intro.lua</a></li>
  <li><a href="../examples/pagination.lua.html">pagination.lua</a></li>
  <li><a href="../examples/ssl.lua.html">ssl.lua</a></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
  <li><a href="../modules/cassandra.html">cassandra</a></li>
  <li><a href="../modules/resty.cassandra.cluster.html">resty.cassandra.cluster</a></li>
  <li><a href="../modules/resty.cassandra.policies.lb.dc_rr.html">resty.cassandra.policies.lb.dc_rr</a></li>
  <li><a href="../modules/resty.cassandra.policies.lb.req_dc_rr.html">resty.cassandra.policies.lb.req_dc_rr</a></li>
  <li><a href="../modules/resty.cassandra.policies.lb.req_rr.html">resty.cassandra.policies.lb.req_rr</a></li>
  <li><a href="../modules/resty.cassandra.policies.lb.rr.html">resty.cassandra.policies.lb.rr</a></li>
  <li><a href="../modules/resty.cassandra.policies.reconnection.const.html">resty.cassandra.policies.reconnection.const</a></li>
  <li><a href="../modules/resty.cassandra.policies.reconnection.exp.html">resty.cassandra.policies.reconnection.exp</a></li>
  <li><a href="../modules/resty.cassandra.policies.retry.simple.html">resty.cassandra.policies.retry.simple</a></li>
</ul>
<h2>Manual</h2>
<ul class="nowrap">
  <li><a href="../manual/README.md.html">README</a></li>
</ul>

</div>

<div id="content">

    <h2>batch.lua</h2>
<pre>
<span class="comment">--------------------------------
</span><span class="comment">-- Single host module, plain Lua
</span><span class="comment">--------------------------------
</span>
<span class="keyword">local</span> cassandra = <span class="global">require</span> <span class="string">"cassandra"</span>

<span class="keyword">local</span> client = <span class="global">assert</span>(cassandra.new {
  host = <span class="string">"127.0.0.1"</span>,
  keyspace = <span class="string">"my_keyspace"</span>
})

client:settimeout(<span class="number">1000</span>)

<span class="global">assert</span>(client:connect())

<span class="global">assert</span>(client:execute <span class="string">[[
  CREATE TABLE IF NOT EXISTS things(
    id uuid PRIMARY KEY,
    n int
  )
]]</span>)

<span class="global">assert</span>(client:execute <span class="string">[[
  CREATE TABLE IF NOT EXISTS counters(
    key text PRIMARY KEY,
    value counter
  )
]]</span>)

<span class="comment">-- Logged batch
</span><span class="keyword">local</span> res = <span class="global">assert</span>(client:batch {
  {<span class="string">"INSERT INTO things(id, n) VALUES(1144bada-852c-11e3-89fb-e0b9a54a6d11, 1)"</span>},
  {<span class="string">"UPDATE things SET n = 2 WHERE id = 1144bada-852c-11e3-89fb-e0b9a54a6d11"</span>},
  {<span class="string">"UPDATE things SET n = 3 WHERE id = 1144bada-852c-11e3-89fb-e0b9a54a6d11"</span>}
})
<span class="global">print</span>(res.<span class="global">type</span>) <span class="comment">-- "VOID"
</span>
<span class="comment">-- Unlogged batch, with binded parameters
</span><span class="keyword">local</span> uuid = <span class="string">"1144bada-852c-11e3-89fb-e0b9a54a6d11"</span>
<span class="keyword">local</span> serialized_uuid = cassandra.uuid(uuid)

res = <span class="global">assert</span>(client:batch({
  {<span class="string">"INSERT INTO things(id, n) VALUES(?, ?)"</span>, {serialized_uuid, <span class="number">4</span>}},
  {<span class="string">"INSERT INTO things(id, n) VALUES(?, ?)"</span>, {serialized_uuid, <span class="number">5</span>}},
  {<span class="string">"INSERT INTO things(id, n) VALUES(?, ?)"</span>, {serialized_uuid, <span class="number">6</span>}},
}), {
  logged = <span class="keyword">false</span>
})
<span class="global">print</span>(res.<span class="global">type</span>) <span class="comment">-- "VOID"
</span>
<span class="comment">-- Counter batch
</span>res = <span class="global">assert</span>(client:batch({
  {<span class="string">"UPDATE counters SET value = value + 1 WHERE key = 'counter'"</span>},
  {<span class="string">"UPDATE counters SET value = value + 1 WHERE key = 'counter'"</span>},
  {<span class="string">"UPDATE counters SET value = value + 1 WHERE key = 'counter'"</span>}
}, {
  counter = <span class="keyword">true</span>
}))
<span class="global">print</span>(res.<span class="global">type</span>) <span class="comment">-- "VOID"
</span>
<span class="comment">-- Batch of prepared queries
</span><span class="keyword">local</span> res1 = <span class="global">assert</span>(client:prepare(<span class="string">"INSERT INTO things(id, n) VALUES(1144bada-852c-11e3-89fb-e0b9a54a6d11, 1)"</span>))
<span class="keyword">local</span> res2 = <span class="global">assert</span>(client:prepare(<span class="string">"UPDATE things set n = 2 WHERE id = 1144bada-852c-11e3-89fb-e0b9a54a6d11"</span>))

<span class="keyword">local</span> res = <span class="global">assert</span>(client:batch({
  {[<span class="number">3</span>] = res1.query_id},
  {[<span class="number">3</span>] = res2.query_id}
}, {
  prepared = <span class="keyword">true</span>
}))
<span class="global">print</span>(res.<span class="global">type</span>) <span class="comment">-- "VOID"
</span>
client:close()</pre>


</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2022-05-20 11:10:11 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
